package com.abso.sunlight.explorer.editors;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

import com.abso.sunlight.api.Legislator;
import com.abso.sunlight.explorer.SunlightImages;

/**
 * A specialized input for the editor able to display a specific legislator.
 */
public class LegislatorDetailsEditorInput implements IEditorInput {

    /** The legislator. */
    private Legislator legislator;

    /**
     * Constructs a new input.
     * 
     * @param legislator   the legislator the editor focus on.
     */
    public LegislatorDetailsEditorInput(Legislator legislator) {
        this.legislator = legislator;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IEditorInput#exists()
     */
    public boolean exists() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
     */
    public ImageDescriptor getImageDescriptor() {
        return SunlightImages.LEGISLATOR.getDescriptor();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IEditorInput#getName()
     */
    public String getName() {
        return StringUtils.join(new String[] { legislator.getFirstName(), legislator.getMiddleName(), legislator.getLastName(),
                legislator.getNameSuffix() }, " ");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IEditorInput#getPersistable()
     */
    public IPersistableElement getPersistable() {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IEditorInput#getToolTipText()
     */
    public String getToolTipText() {
        return getName();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    public Object getAdapter(Class adapter) {
        return null;
    }

    /**
     * Returns the legislator.
     * 
     * @return the legislator.
     */
    public Legislator getLegislator() {
        return legislator;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof LegislatorDetailsEditorInput) {
            return ObjectUtils.equals(legislator, ((LegislatorDetailsEditorInput) obj).legislator);
        }
        return false;
    }

}
